package com.medical.controller;

import com.github.pagehelper.PageInfo;
import com.medical.pojo.Handle;
import com.medical.pojo.Limitorder;
import com.medical.pojo.Process;
import com.medical.pojo.Shippingapplication;
import com.medical.service.HandleService;
import com.medical.service.LimitService;
import com.medical.service.ProcessService;
import com.medical.service.ShippingService;
import com.medical.vo.Rmed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author Jerry.xu
 * @date 2024/11/25
 */

@RestController
@RequestMapping("/shipping")
public class ShippingController {
    @Autowired
    private ShippingService shippingService;
    @Autowired
    private ProcessService processService;
    @Autowired
    private LimitService limitService;
    @Autowired
    private HandleService handleService;
    // 查询所有符合条件的发货申请信息
    @RequestMapping("findAll")
    public Rmed findAll(@RequestBody Shippingapplication shippingapplication, Integer pageNo, Integer pageSize){
        // 调用shippingService的findAll方法查询符合条件的发货申请信息，并分页返回
        PageInfo pageInfo = shippingService.findAll(shippingapplication,pageNo,pageSize);
        // 返回查询结果，包括分页信息
        return Rmed.suceessData(pageInfo);
    }

    // 添加Shippingapplication对象
    @RequestMapping("add")
    public Rmed add(@RequestBody Shippingapplication shippingapplication){
        try {
            // 当shippingapplication的ID为null时，调用shippingService的add方法添加该对象
            if (shippingapplication.getId()==null){
                shippingService.add(shippingapplication);
            }
            // 操作成功，返回成功信息
            return Rmed.suceessData("yes");
        } catch (Exception e) {
            // 异常处理：打印异常信息
            e.printStackTrace();
        }
        // 操作失败，返回错误信息
        return Rmed.error("no");
    }

    // 根据ID查询Shippingapplication对象
    @RequestMapping("findById")
    public Rmed findById(Integer id){
        // 调用shippingService的findById方法查询对象，并使用Rmed.suceessData封装查询结果
        return Rmed.suceessData(shippingService.findById(id));
    }

    //根据状态码判断是否退回
    @RequestMapping("return")
    public Rmed returnshipping(@RequestBody Process process){
        try {
            processService.add(process);
            // 操作成功，返回成功信息
            return Rmed.suceessData("yes");
        } catch (Exception e) {
            // 异常处理：打印异常信息
            e.printStackTrace();
        }
        // 操作失败，返回错误信息
        return Rmed.error("no");
    }

    @RequestMapping("update")
    public Rmed update(@RequestBody Shippingapplication shippingapplication){
        try {
            // 调用shippingService的update方法更新对象
            shippingService.update(shippingapplication);
            // 操作成功，返回成功信息
            return Rmed.suceessData("yes");
        } catch (Exception e) {
            // 异常处理：打印异常信息
            e.printStackTrace();
        }
        // 操作失败，返回错误信息
        return Rmed.error("no");
    }

    //条件查询
    @RequestMapping("findByCondition")
    public Rmed findByCondition(@RequestBody Shippingapplication shippingapplication){
        return Rmed.suceessData(shippingService.findByCondition(shippingapplication));
    }

    @RequestMapping("handle")
    public Rmed returnshipping(@RequestBody Handle handle){
        try {
            handleService.add(handle);
            // 操作成功，返回成功信息
            return Rmed.suceessData("yes");
        } catch (Exception e) {
            // 异常处理：打印异常信息
            e.printStackTrace();
        }
        // 操作失败，返回错误信息
        return Rmed.error("no");
    }

    @RequestMapping("limit")
    public Rmed returnshipping(@RequestBody Limitorder limitorder){
        try {
            limitService.add(limitorder);
            // 操作成功，返回成功信息
            return Rmed.suceessData("yes");
        } catch (Exception e) {
            // 异常处理：打印异常信息
            e.printStackTrace();
        }
        // 操作失败，返回错误信息
        return Rmed.error("no");
    }
    @RequestMapping("findByIds")
    public Rmed findByIds(Integer id){
        // 调用shippingService的findById方法查询对象，并使用Rmed.suceessData封装查询结果
        return Rmed.suceessData(shippingService.findByIds(id));
    }

    @RequestMapping("listname")
    public Rmed listname(String name){
        return Rmed.suceessData(shippingService.listname(name));
    }
}
